﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pibby.Core.IO;
using Pibby.Core.Persistence;
using Pibby.Core.Playlist;
using Pibby.Core.PortableMedia;
using invrt;

namespace Pibby.Core
{
	public class TransferableCollectionBuilder
	{
		public TransferableCollectionBuilder()
		{
			this.Margin = 1024 * 1024;
		}

		public long Margin
		{
			get;
			set;
		}

		public TransferableCollection Build(IFolder sourcefolder, IFolder targetFolder)
		{
			MediaPlaylist playlist = MediaPlaylist.LoadFrom(sourcefolder);

			return this.Build(playlist, targetFolder);
		}

		public TransferableCollection Build(IPlaylist playlist, IFolder targetFolder)
		{
			var colleciton = new TransferableCollection();

			foreach (PlaylistItem item in playlist.Items)
			{
				colleciton.Add (new Transferable () 
				{ 
					Id = item.Id,
					Source = System.IO.Path.Combine(targetFolder.Path, item.Path)
				});
			}

			return colleciton;
		}

		public TransferableCollection Build(IPodcastRepository repository, IFolder downloadFolder, IFolder deviceFolder, PortableMediaDevice mp3Player)
		{
			// for each pc - list content...
			var podcasts = repository.LoadPodcasts();
			podcasts.SortByWhuffie();

			string baseFolder = mp3Player.Path;

			IDrive drive = mp3Player.Drive;

			long availableSpace = 0;

			if (drive != null)
			{
				availableSpace = drive.AvailableFreeSpace;
			}

			var collection = new TransferableCollection();

			foreach (var podcast in podcasts)
			{
				var folder = downloadFolder.Append(podcast.SafeName);

				var downloadsLister = new FileListVisitor();
				downloadsLister.ListContentsOf(folder,
												System.IO.SearchOption.TopDirectoryOnly,
												FileConstants.Mp3FileFilter);

				if (downloadsLister.Items.Count > 0)
				{
					downloadsLister.Items.SortBySize();
					var episodes = repository.LoadEpisodes(podcast.PrimaryKey);

					foreach (var item in downloadsLister.Items)
					{
						// find it in the list of episodes for this podcast...
						var episode = episodes.FindByFileName(item.Name);

						if (episode != null)
						{
							// can we safely transfer this file???
							if (availableSpace > (item.Length + this.Margin))
							{
								collection.Add(new Transferable
								{
									Id = episode.Id,
									FriendlyName = episode.FileName,
									Size = item.Length,
									Source = item.Path,
									TargetFolder = mp3Player.SuggestPathUsing(podcast.SafeName),
									Tag = new FileCopyCookie()
									{
										Id = episode.PrimaryKey.Value,
										Name = episode.FileName
									}
								});

								availableSpace -= item.Length;
							}

							if (availableSpace < this.Margin)
								break;
						}
					}
				}
			}

			// check the download folder...
			if (availableSpace > this.Margin)
			{
				var folder = downloadFolder.Append("Download");

				var downloadsLister = new FileListVisitor();
				downloadsLister.ListContentsOf(folder,
												System.IO.SearchOption.TopDirectoryOnly,
												FileConstants.Mp3FileFilter);

				if (downloadsLister.Items.Count > 0)
				{
					downloadsLister.Items.SortBySize();

					foreach (var item in downloadsLister.Items)
					{
						// can we safely transfer this file???
						if (availableSpace > (item.Length + this.Margin))
						{
							collection.Add(new Transferable
							{
								Id = Guid.NewGuid().ToString(),
								FriendlyName = item.Name,
								Size = item.Length,
								Source = item.Path,
								TargetFolder = mp3Player.SuggestPathUsing("Download"),
							});

							availableSpace -= item.Length;
						}

						if (availableSpace < this.Margin)
							break;
					}
				}
			}

			return collection;
		}
	}
}
